//-----------------------------------------------------------------------
// <copyright file="MajorMarketDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class MajorMarketDAOBase
    {
        #region Common methods
        public virtual MajorMarket CreateMajorMarketFromReader(IDataReader reader)
        {
            MajorMarket item = new MajorMarket();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MajorMarketID"))) item.MajorMarketID = (int)reader["MajorMarketID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) item.SortOrder = (int)reader["SortOrder"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMajorMarketFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateMajorMarket methods
            
        public virtual void CreateMajorMarket(MajorMarket majorMarket)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, majorMarket.Title);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int32, majorMarket.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, majorMarket.Enable);
                database.AddOutParameter(dbCommand, "@MajorMarketID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                majorMarket.MajorMarketID = (int)database.GetParameterValue(dbCommand, "@MajorMarketID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateMajorMarketException, ex);
            }
        }

        #endregion

        #region UpdateMajorMarket methods
        
        public virtual void UpdateMajorMarket(MajorMarket majorMarket)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketUpdate");            
                
                database.AddInParameter(dbCommand, "@MajorMarketID", DbType.Int32, majorMarket.MajorMarketID);
                database.AddInParameter(dbCommand, "@Title", DbType.String, majorMarket.Title);
                database.AddInParameter(dbCommand, "@SortOrder", DbType.Int32, majorMarket.SortOrder);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, majorMarket.Enable);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateMajorMarketException, ex);
            }
        }
        
        #endregion

        #region DeleteMajorMarket methods
        public virtual void DeleteMajorMarket(int majorMarketID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketDelete");
                
                database.AddInParameter(dbCommand, "@MajorMarketID", DbType.Int32, majorMarketID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteMajorMarketException, ex);
            }
        }

        #endregion

        #region GetMajorMarket methods
        
        public virtual MajorMarket GetMajorMarket(int majorMarketID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketGet");
                
                database.AddInParameter(dbCommand, "@MajorMarketID", DbType.Int32, majorMarketID);
                
                MajorMarket majorMarket =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        majorMarket = CreateMajorMarketFromReader(reader);
                        reader.Close();
                    }
                }
                return majorMarket;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMajorMarketException, ex);
            }
        }
        
        #endregion

        #region GetMajorMarketList methods
        public virtual MajorMarketCollection GetMajorMarketList(MajorMarketColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MajorMarketCollection majorMarketCollection = new MajorMarketCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MajorMarket majorMarket = CreateMajorMarketFromReader(reader);
                        majorMarketCollection.Add(majorMarket);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return majorMarketCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMajorMarketListException, ex);
            }
        }
        
        public virtual MajorMarketCollection GetMajorMarketList(MajorMarketColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetMajorMarketList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetMajorMarketList methods
        public virtual MajorMarketCollection GetMajorMarketListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spMajorMarketGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                MajorMarketCollection majorMarketCollection = new MajorMarketCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        MajorMarket majorMarket = CreateMajorMarketFromReader(reader);
                        majorMarketCollection.Add(majorMarket);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return majorMarketCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetMajorMarketListException, ex);
            }
        }
        
        public virtual MajorMarketCollection GetMajorMarketListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetMajorMarketListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}